package com.project.service.impl;

import com.project.entity.po.Entry;
import com.project.exception.ServiceException;
import com.project.mapper.EntryMapper;
import com.project.result.ResponseResult;
import com.project.result.ResultCode;
import com.project.service.EntryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class EntryServiceImpl implements EntryService {
    @Resource
    EntryMapper entryMapper;


    @Override
    public ResponseResult<List<Entry>> selectAllEntry() {
        List<Entry> list = entryMapper.selectAllEntry();
        if(list.size() == 0){
            throw new ServiceException(ResultCode.ERROR);
        }
        return ResponseResult.success(ResultCode.SUCCESS,list);
    }

    @Override
    public ResponseResult<List<Entry>> selectEntryByName(String name) {
        List<Entry> list = entryMapper.selectEntryByName(name);
        if(list.size() == 0){
            throw new ServiceException(ResultCode.ERROR);
        }
        return ResponseResult.success(ResultCode.SUCCESS,list);
    }

    @Override
    public ResponseResult<Integer> updateEntry(Entry entry) {
        int i = entryMapper.updateEntry(entry);
        if(i == 0){
            throw new ServiceException(ResultCode.ERROR);
        }
        return ResponseResult.success(ResultCode.SUCCESS,i);
    }

    @Override
    public ResponseResult<Integer> updateEntryById(Long id) {
        int i = entryMapper.updateEntryById(id);
        if(i == 0){
            throw new ServiceException(ResultCode.ERROR);
        }
        return ResponseResult.success(ResultCode.SUCCESS,i);
    }

    @Override
    public ResponseResult<Integer> insertEntry(Entry entry) {
        int i = entryMapper.insertEntry(entry);
        if(i == 0){
            throw new ServiceException(ResultCode.ERROR);
        }
        return ResponseResult.success(ResultCode.SUCCESS,i);
    }
}
